import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
import router from "@src/router";
import base from "@src/store/base";
import { getRenewInfo, renewMember } from "@src/api/member";
import memberPayBehavior from "@src/common/memberPayBehavior";
import member from "@src/store/member";
import dayjs from "dayjs";

Component({
  behaviors: [storeBindingsBehavior, router.routerBehavior, memberPayBehavior],
  storeBindings: [{
    store: member,
    actions: ['getBaseInfo'],
  }, {
    store: base,
    fields: ['themeColor', 'themeTitleColor']
  }],
  data: {},
  methods: {
    onLoad() {
      this.fetchData();
    },
    fetchData() {
      getRenewInfo().then(res => {
        if (res.code === 0) {
          let renewInfoData = res.data
          renewInfoData.nowExpireTime = dayjs(renewInfoData.nowExpireTime).format('YYYY-MM-DD')
          renewInfoData.newExpireTime = dayjs(renewInfoData.newExpireTime).format('YYYY-MM-DD')
          this.setData({ renewInfoData })
        } else {
          wx.showModal({
            title: '续费提醒',
            content: res.msg,
            showCancel: false,
            confirmText: '好的',
            success(res) {
              if (res.confirm) {
                router.push({ name: 'index' })
              }
            }
          })
        }
      })
    },
    handleConfirm() {
      wx.showLoading({ title: "续费中...", mark: true });
      renewMember().then(res => {
        wx.hideLoading();
        console.info(res)
        this.setData({ payFee: res.data.payFee, orderNo: res.data.orderNo });
        let payData = {
          orderNo: res.data.orderNo,
          payFee: res.data.payFee,
          payChannels: ["balance", 'wx'],
          hasDiscount: false
        }
        this.toMemberPay(payData).then(res => {
          // 刷新会员卡数据
          this.getBaseInfo()
          wx.showModal({
            title: '续费提醒',
            content: `续费成功，新的有效期至${this.data.renewInfoData.newExpireTime}`,
            showCancel: false,
            confirmText: '好的',
            success(res) {
              if (res.confirm) {
                router.push({ name: "mine" })
              }
            }
          })
        })
      })
    }
  }
})
